/**
 * FileName: answer_2
 * Author:   why
 * Date:     2019/3/12 17:47
 * Description: 两次hash
 */
package algorithm.leetcode.problem_1;

import java.util.HashMap;
import java.util.Map;

public class answer_2 {
    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> mp = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            //存入map中
            mp.put(nums[i], i);
        }
        for (int i = 0; i < nums.length; i++) {
            int number = target - nums[i];
            //在map中找对应的补码
            if (mp.containsKey(number) && mp.get(number) != i) {
                return new int[]{i, mp.get(number)};
            }
        }
        throw new IllegalArgumentException("no answer_2 sum solution");
    }
}
